package iammry.com.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import iammry.com.R;
import iammry.com.base.BaseActivity;
import iammry.com.mode.ChartType;
import iammry.com.ui.AverageCubicTemperatureChart;
import iammry.com.ui.ChartService;

/**
 * Created by kim
 * on 2016/11/6 0006.
 */
public class ChartActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.ll_back)
    LinearLayout ll_back;
    @ViewInject(R.id.tv_week_text)
    TextView tvWeekText;
    @ViewInject(R.id.tv_moth_text)
    TextView tvMothText;
    @ViewInject(R.id.ll_chart_content)
    LinearLayout llChartContent;
    ChartService chartService;
    Timer timer;
    List<Double[]> xList1;
    List<Double[]> yList1;
    double[] value1 = new double[]{40, 30, 50, 25, 80, 60, 45};
    double[] value2 = new double[]{20, 60, 30, 25, 70, 90, 10};
    List<double[]> values = new ArrayList<double[]>();

    AverageCubicTemperatureChart averageCubicTemperatureChart;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //刷新图表
            averageCubicTemperatureChart.updateChart();
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setLayout(this, R.layout.chartactivity_layout);
    }

    @Override
    protected void initData() {
        chartService = new ChartService(this);
        xList1 = new ArrayList<>();
        yList1 = new ArrayList<>();
        timer = new Timer();
        swtichView(1);

    }

    @Override
    protected void initEvent() {
        tvWeekText.setOnClickListener(this);
        tvMothText.setOnClickListener(this);
        ll_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_week_text:
                swtichView(1);
                break;

            case R.id.tv_moth_text:
                swtichView(2);
                break;

            case R.id.ll_back:
                finish();
                break;
        }
    }


    int count;

    //切换View
    public void swtichView(int type) {

        switch (type) {

            case 1:
                llChartContent.removeAllViews();
                count = 0;
                tvWeekText.setTextColor(Color.WHITE);
                tvWeekText.setBackgroundResource(R.drawable.rectangle_02);
                tvMothText.setTextColor(Color.BLACK);
                tvMothText.setBackgroundResource(R.drawable.rectangle_03);
                //获取图表
                averageCubicTemperatureChart = new AverageCubicTemperatureChart();
                View view = averageCubicTemperatureChart.getView(ChartActivity.this, ChartType.WEEK, new double[]{}, new double[]{});
                llChartContent.addView(view);
/*
                TimerTask   task = new TimerTask() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = 200;
                        handler.sendMessage(message);
                    }
                };
                timer.schedule(task, 1000,1000);*/

                break; 

            case 2:
                llChartContent.removeAllViews();
                tvWeekText.setTextColor(Color.BLACK);
                tvWeekText.setBackgroundResource(R.drawable.rectangle_01);
                tvMothText.setTextColor(Color.WHITE);
                tvMothText.setBackgroundResource(R.drawable.rectangle_04);
                View view1 = new AverageCubicTemperatureChart().getView(this, ChartType.MONTH, new double[]{}, new double[]{});
                llChartContent.addView(view1);
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();

        }
    }


}
